/*
 * Copyright (c) 2008, Artur Emagulov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/**
 * @author Artur Emagulov
 * 
 *         Kiddie-OS Java UART driver example
 * 
 */
public class UartDrv {

	private byte[] Hello = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l',
			'd', '!', '\n' };
	private byte[] data;
	private short[] data2;
	private byte[] _data;
	private int[] data3;

	/**
	 * Constructor
	 */
	public UartDrv() {
		byte q;
		data = _palloc(0x16000000, 0x1000); /* map uart_base */
		_data = _palloc(0x14000000, 0x1000); /* map ic base */
		data2 = ba2sa(data);
		data3 = ba2ia(_data);
		initialize();
		for (int i = 0; i < Hello.length; i++) {
			q = putchar((byte) Hello[i]);
		}
	}

	/**
	 * This initializes UART
	 */
	int initialize() {
		int val;
		byte val_bh;
		byte val_bl;

		int uart_flags;
		int temp;
		int divider;
		int remainder;
		int fraction;

		/* Initialize UART */
		/* Disable UART0 */
		data2[UartDrv.UART_CTRL_OFFST / 2] = 0x0;

		/*
		 * Wait for the end of transmission or reception of the current
		 * character.
		 */
		do {
			uart_flags = (int) data2[UartDrv.UART_FR_OFFST / 2];
		} while ((uart_flags & UART_TX_BUSY) != 0);

		/* Setup FIFO interrupt level */
		val = (2 << 3) | (2 << 0);
		data2[UartDrv.UART_IFLS_OFFST / 2] = (short) val;

		/* Setup clock */
		data2[UartDrv.UART_BDR_OFFST / 2] = 1;
		data2[UartDrv.UART_FDR_OFFST / 2] = 0;
		data2[UartDrv.UART_LCTRL_HIGH_OFFST / 2] = 0;
		data2[0] = 0;

		/*
		 * Wait for the end of transmission or reception of the current
		 * character.
		 */
		do {
			uart_flags = (int) data2[UartDrv.UART_FR_OFFST / 2];
		} while ((uart_flags & UART_TX_BUSY) != 0);

		/* Clear RX interrupts */
		val = (1 << 4);
		data2[UartDrv.UART_ICR_OFFST / 2] = (short) val;

		/* Enable RX, RT interrupts */
		val = (1 << 4);
		data2[UartDrv.UART_IMSC_OFFST / 2] = (short) val;

		/* Reprogram the UART: Enable UART RX, TX */
		val = ((1 << 8) | (1 << 9) | (1 << 0));
		data2[UartDrv.UART_CTRL_OFFST / 2] = (short) val;

		/* Enable KBD_INT */
		val = data3[2];
		data3[2] = val | 0x40 | 0x40 | 0x8 | 0x1FC007FF;
		return 0;
	}

	/************************************************************************
	 * 
	 */
	public static byte[] _palloc(int addr, int size) {
		return palloc(addr, size);
	}

	private byte putchar(byte b) {
		data2[0] = b;
		return b;
	}

	/** maps physical memory from 'addr' to virtual memory space */
	private static native byte[] palloc(int addr, int size);

	/** converts byte array to short array */
	private static native short[] ba2sa(byte[] ba);

	/** converts byte array to int array */
	private static native int[] ba2ia(byte[] ba);

	/** takes physical address of byte array */
	private static native int ba2paddr(byte[] ba);

	/** takes virtual address of byte array */
	private static native int ba2vaddr(byte[] ba);

	/************************************************************************
	 * Constants
	 */
	public static final int UART_CTRL_OFFST = 0x30;
	public static final int UART_FR_OFFST = 0x18;
	public static final int UART_TX_BUSY = 0x8;
	public static final int UART_IFLS_OFFST = 0x34;
	public static final int UART_BDR_OFFST = 0x24;
	public static final int UART_FDR_OFFST = 0x28;
	public static final int UART_LCTRL_HIGH_OFFST = 0x2c;
	public static final int UART_ICR_OFFST = 0x44;
	public static final int UART_IMSC_OFFST = 0x38;
}
